//app.js
App({
  _loginCallback: [],
  logining: false,
  globalData: {
    statusText: {
      'all': '全部订单',
      '0': '待确认',
      '1': '待排期',
      '2': '待拍摄',
      '3': '未传选片',
      '4': '选片中',
      '5': '待修片',
      '6': '待制作',
      '7': '待发货',
      '8': '已完成',
    },
    statusMap: {
      '0': {
        text: '待确认',
        actions: [{
            text: '作废订单',
            action: 'removeOrder'
          },
          {
            type: 'primary',
            text: '确认订单',
            action: 'redirect',
            url: '../newOrder/newOrder'
          },
          // {
          //   type: 'primary',
          //   text: '直接确认',
          //   action: 'changeStatus',
          //   url: 1
          // },


        ]
      },
      "1": {
        text: '待排期',
        actions: [{
          type: 'primary',
          text: '派单排期',
          action: 'redirect',
          url: '../newSchedule/newSchedule'
        }, ]
      },
      '2': {
        text: '待拍摄',
        actions: [{
            text: '重排档期',
            action: 'redirect',
            url: '../newSchedule/newSchedule'
          },
          {
            type: 'primary',
            text: '拍摄完毕',
            action: 'changeStatus',
            url: 3
          },

        ]
      },
      '3': {
        text: '未传选片',
        actions: [{
            text: '补拍排期',
            action: 'redirect',
            url: '../newSchedule/newSchedule'
          },
          {
            type: 'primary',
            text: '上传选片',
            action: 'redirect',
            url: '../../choose/choose'
          },
        ]
      },
      '4': {
        text: '选片中',
        actions: [{
            text: '查看选片',
            action: 'redirect',
            url: '../../choose/choose'
          },
          {
            type: 'primary',
            text: '结束选片',
            action: 'changeStatus',
            url: 5
          },
        ]
      },
      '5': {
        text: '待修片',
        actions: [
          // {
          //   text: '重新开放选片',
          //   action: 'changeStatus',
          //   url: 4
          // },
          {
            text: '查看选片',
            action: 'redirect',
            url: '../../choose/choose'
          },
          {
            type: 'primary',
            text: '修片完毕',
            action: 'changeStatus',
            url: 6
          },
        ]
      },
      '6': {
        text: '待制作',
        actions: [{
            text: '查看选片',
            action: 'redirect',
            url: '../../choose/choose'
          },
          {
            type: 'primary',
            text: '制作完毕',
            action: 'changeStatus',
            url: 7
          }
        ],

      },

      '7': {
        text: '待发货',
        actions: [{
            text: '查看选片',
            action: 'redirect',
            url: '../../choose/choose'
          },
          {
            type: 'primary',
            text: '发货完毕',
            action: 'changeStatus',
            url: 8
          }
        ],

      },
      '8': {
        text: '已完成',
        actions: [{
          text: '查看选片',
          action: 'redirect',
          url: '../../choose/choose'
        }, ],
      },

    }
  },

  login: function (callback) {   
    if (this.globalData.user) {    
      console.log('111111111')
      callback(this.globalData.user)   
    } else {   
      console.log('222222222') 
      this.relogin(callback);
    }
  },

  // 强制重新登录，在注册和绑定之后调用
  relogin: function (callback) {
    const that = this;
    if (callback) {
      this._loginCallback.push(callback);
    }
    if (this.logining) return;
    this.logining = true;
    wx.cloud.callFunction({
      name: 'login',
      success: res => {
        // console.log('user',res.result);
        that.globalData.user = res.result;
        let callback = that._loginCallback.pop();
        while (callback) {
          callback(that.globalData.user);
          callback = that._loginCallback.pop();
        }
        this.logining = false;
      },
      fail: err => {
        console.error(err);
      }
    })
  },
  getSetiings() {
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'get',
        table: 'setting',
      },
      success: res => {
        this.globalData.settings = res.result.data[0]
      }
    })
  },
  requestOrderSubscribeMessage() {
    const templateId = ['fgN7PR9idyvoDi2-l0JW-ObwnAY5cRIfz1oA002FipU', 'uzadlWcZzW4JC3rVj-vHpuVrkcR-aQ38vrpspE5gNDY', 'd2Fy2BPP1uRDHpvrsazOqDV3g4POTR9_kcOmGgYTT10']
    wx.requestSubscribeMessage({
      tmplIds: templateId,
      success: (res2) => {
        console.log(res2)
       // fn()
        //  this.onShow()
      },
      fail: (err) => {
        console.log('msg', `失败（${JSON.stringify(err)}）`)

      },
      complete: (res) => {
      
      }
    })

  },
  onLaunch: function (options) {

    let that = this;
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({       
        traceUser: true,
      })

      wx.getSystemInfo({
        success: (result) => {
          console.log(result)
          let systemInfo = {
            windowHeight: result.windowHeight,
            windowWidth: result.windowWidth,
            pixelRatio: result.pixelRatio,
            platform: result.platform
          }
          this.globalData.systemInfo = systemInfo
        },
      })
      this.relogin()
      this.getSetiings()
    }



    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log("系统存在新版本：", res.hasUpdate)
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    });
  }
})